import os
from pydub import AudioSegment

# 指定文件夹路径
folder_path = 'output_wav_files'

# 创建一个空的 AudioSegment 对象
combined = AudioSegment.empty()

# 获取所有 WAV 文件
wav_files = [f for f in os.listdir(folder_path) if f.endswith('.wav')]

# 按文件名中的数字进行排序
wav_files.sort(key=lambda x: int(x.split('_')[1].split('.')[0]))

# 遍历文件并合并
for file_name in wav_files:
    file_path = os.path.join(folder_path, file_name)
    segment = AudioSegment.from_wav(file_path)
    combined += segment

# 保存合并后的文件
combined.export('combined_output.wav', format='wav')
